<?php

namespace App\Controller\Admin;

use App\Controller\Example\BannerExample;
use App\Models\AdminModel;
use App\Models\BannerModel;
use App\Models\Conn;
use Core\Common\Common;
use Core\Enums\ErrorEnum;
use Core\Result\Result;

class Banner extends BannerModel
{
    /**
     * 管理后台轮播图列表
     * entry token,startTime,endTime,page,limit
     * return json
     * */
    public function bannerList($data){
        $assemble = Common::assemble(new BannerExample(), $data);
        $identical = Common::identical($data);
        $conn = new Conn();
        $joinSql = " WHERE `status` >=0 ";
        if($assemble->startTime !=null){ $joinSql.=" and UNIX_TIMESTAMP(`time`) >= '$assemble->startTime' ";}
        if($assemble->endTime !=null){ $joinSql.=" and UNIX_TIMESTAMP(`time`) <= '$assemble->endTime' ";}
        $userSql = "SELECT `id`, `type`, `img`, `link`, `position`, `bg_color`, `time`,`status` FROM `banner` $joinSql order by id desc limit $identical->pageStart,$identical->limit";
        $readConn = $conn->readConn($userSql,true);
        foreach ($readConn as $item=>$value){
            if($value["status"] ==1){
                $readConn[$item]["status"]="显示";
            }else{
                $readConn[$item]["status"]="隐藏";
            }
        }
        $returnData["list"]=$readConn;
        $userCountSql="SELECT count(id) as count FROM `banner` $joinSql";
        $returnData["count"] = $conn->readConn($userCountSql)["count"];
        $returnData["total"] = null;
        Result::success($returnData);
    }
    /**
     * 管理后台添加轮播图
     * entry token,type,img,link,position,bg_color,status
     * return json
     * */
    public function addBanner($data){
        $assemble = Common::assemble(new BannerExample(), $data);
        $conn = new Conn();
        $adminInToken = AdminModel::getAdminInToken($conn, $assemble->token);
        $conn->shiwuStart();
        if(self::insertBanner($conn,$assemble) !=1){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::SUBMITFAILED);
        }
        $conn->shiwuEnd();

        AdminLog::addAdminLog($adminInToken["username"],"轮播图","添加轮播图:[]");
        Result::success("ok");
    }
    /**
     * 管理后台编辑轮播图
     * entry token,id,type,img,link,position,bg_color,status
     * return json
     * */
    public function editBanner($data){
        $assemble = Common::assemble(new BannerExample(), $data);
        $conn = new Conn();
        $adminInToken = AdminModel::getAdminInToken($conn, $assemble->token);
        $conn->shiwuStart();
        if(self::upBannerInside($conn,$assemble) !=1){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::SUBMITFAILED);
        }
        $conn->shiwuEnd();
        AdminLog::addAdminLog($adminInToken["username"],"资产包","编辑资产包:[".$assemble->id."]");
        Result::success("ok");
    }
}